{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/138_draw_control.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Clip Earth Engine images interactively with the Draw Control**\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install -U geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import ipywidgets as widgets"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "Add some Earth Engine data to the map, then you can use the draw control and a button widget to clip the Earth Engine data by the drawn polygons. \n",
    "\n",
    "The drawn geometries can be retrieved as either an ee.Geometry object (`m.user_roi`) or ee.FeatureCollection (`m._user_rois`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "self = geemap.Map(center=[0, 9.31], zoom=3)\n",
    "image = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "vis_params = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 6000,\n",
    "    \"palette\": \"terrain\",\n",
    "}\n",
    "self.add_layer(image, vis_params, \"SRTM\")\n",
    "\n",
    "clip_btn = widgets.Button(description=\"Clip Image\")\n",
    "reset_btn = widgets.Button(description=\"Reset\")\n",
    "\n",
    "\n",
    "def on_clip_btn_clicked(b):\n",
    "    if self.user_roi is not None:\n",
    "        try:\n",
    "            clipped_image = image.clip(self.user_roi)\n",
    "            self.add_layer(clipped_image, vis_params, \"Clipped Image\")\n",
    "            self.find_layer(\"SRTM\").visible = False\n",
    "        except:\n",
    "            pass\n",
    "\n",
    "\n",
    "clip_btn.on_click(on_clip_btn_clicked)\n",
    "\n",
    "\n",
    "def on_reset_btn_clicked(b):\n",
    "    self._draw_control.clear()\n",
    "    self.find_layer(\"SRTM\").visible = True\n",
    "    self.layers = self.layers[:3]\n",
    "\n",
    "\n",
    "reset_btn.on_click(on_reset_btn_clicked)\n",
    "\n",
    "widget = widgets.VBox([clip_btn, reset_btn])\n",
    "self.add_widget(widget, position=\"bottomright\")\n",
    "self"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "Create a solara web app."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import solara\n",
    "import ipywidgets as widgets\n",
    "\n",
    "\n",
    "class Map(geemap.Map):\n",
    "    def __init__(self, **kwargs):\n",
    "        super().__init__(**kwargs)\n",
    "\n",
    "        image = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "        vis_params = {\n",
    "            \"min\": 0,\n",
    "            \"max\": 6000,\n",
    "            \"palette\": \"terrain\",\n",
    "        }\n",
    "        self.add_layer(image, vis_params, \"SRTM\")\n",
    "\n",
    "        clip_btn = widgets.Button(description=\"Clip Image\")\n",
    "        reset_btn = widgets.Button(description=\"Reset\")\n",
    "\n",
    "        def on_clip_btn_clicked(b):\n",
    "            if self.user_roi is not None:\n",
    "                try:\n",
    "                    clipped_image = image.clip(self.user_roi)\n",
    "                    self.add_layer(clipped_image, vis_params, \"Clipped Image\")\n",
    "                    self.find_layer(\"SRTM\").visible = False\n",
    "                except:\n",
    "                    pass\n",
    "\n",
    "        clip_btn.on_click(on_clip_btn_clicked)\n",
    "\n",
    "        def on_reset_btn_clicked(b):\n",
    "            self._draw_control.clear()\n",
    "            self.find_layer(\"SRTM\").visible = True\n",
    "            self.layers = self.layers[:3]\n",
    "\n",
    "        reset_btn.on_click(on_reset_btn_clicked)\n",
    "\n",
    "        widget = widgets.VBox([clip_btn, reset_btn])\n",
    "        self.add_widget(widget, position=\"bottomright\")\n",
    "\n",
    "\n",
    "@solara.component\n",
    "def Page():\n",
    "    with solara.Column(style={\"min-width\": \"500px\"}):\n",
    "        Map.element(\n",
    "            center=[0, 9.31],\n",
    "            zoom=3,\n",
    "            height=\"600px\",\n",
    "        )\n",
    "\n",
    "\n",
    "Page()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
